﻿package fpc.styles;
import flash.text.StyleSheet;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;

class ExtendedTextFormat extends TextFormat
{
	public function new(
		?font:String, ?size:Null<Float>, ?color:Null<UInt>,
		?bold:Null<Bool>, ?italic:Null<Bool>, ?underline:Null<Bool>,
		?url:String, ?target:String,
		?align:TextFormatAlign, ?leftMargin:Null<Float>, ?rightMargin:Null<Float>, ?indent:Null<Float>, ?leading:Null<Float>
	)
	{
		super(font, size, color, bold, italic, underline, url, target, align, leftMargin, rightMargin, indent, leading);
	}
	
	public function setFont(font:String, embedFonts:Bool = false)
	{
		this.font = font;
		this.embedFonts = embedFonts;
		return this;
	}
	
	public function setParameters(size:Int, color:UInt = 0x000000, bold:Bool = false, italic:Bool = false, underline:Bool = false)
	{
		this.size = size;
		this.color = color;
		this.bold = bold;
		this.italic = italic;
		this.underline = underline;
		return this;
	}
	
	public function setStyleSheet(styleSheet:StyleSheet)
	{
		this.styleSheet = styleSheet;
		return this;
	}
	
	public function setFilters(filters:Array<Dynamic>)
	{
		this.filters = filters;
		return this;
	}
	
	public function clone()
	{
		var format = new ExtendedTextFormat(
			font, size, color, bold, italic, underline, url, target, align, leftMargin, rightMargin, indent, leading
		);
		format.blockIndent = blockIndent;
		format.bullet = bullet;
		format.display = display;
		format.kerning = kerning;
		format.letterSpacing = letterSpacing;
		format.tabStops = tabStops;
		
		format.styleSheet = styleSheet;
		format.embedFonts = embedFonts;
		format.filters = filters;
		
		return format;
	}
	
	public function applyTo(tf:TextField)
	{
		tf.setTextFormat(this);
		tf.defaultTextFormat = this;
		tf.styleSheet = styleSheet;
		tf.embedFonts = embedFonts;
		tf.filters = filters;
	}
	
	static var _emptyTextFormat:TextFormat;
	
	public static function clearTextFormat(tf:TextField)
	{
		if (_emptyTextFormat == null)
		{
			_emptyTextFormat = new TextFormat();
		}
		tf.setTextFormat(_emptyTextFormat);
		tf.defaultTextFormat = _emptyTextFormat;
		tf.styleSheet = null;
		tf.embedFonts = false;
		tf.filters = null;
	}
	
	public var styleSheet:StyleSheet;
	
	public var embedFonts:Bool;
	
	public var filters:Array<Dynamic>;
}